2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_spi_ex1_3WireMaster.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 /*******************************************************************************
33  * MSP430i2xx EUSCI_B SPI - 3 Wire Incrementing Data Master
34  *
35  * Description: In this example, the device is configured as a SPI master using
36  * ACLK as the clock source. The SPI communication is setup to run at 16kHz.
37  * The initial data is written to the TX Buffer, once the data is sent out the
38  * device gets an RX interrupt that contains the data it recieved from the
39  * slave. The slave and master data values should stay in sync and if there is
40  * a problem in communication the LED will turn on. The device stays in LPM0
41  * and sends the data in the ISR. To properly demo this example connect to
42  * slave version of this example and start the slave device first.
43  *
44  * MSP430i2041
45  * ------------------
46  * /|\| |
47  * | | P1.4|--->LED
48  * --|RST |
49  * | P1.7/UCB0SIMO|--->Data Out
50  * | |
51  * | P1.6/UCB0MISO|<---Data In
52  * | |
53  * | P1.5/UCB0CLK|--->Serial Clock
54  *
55  * Author: Zack Lalanne
56  ******************************************************************************/
57 #include "driverlib.h"
58 
59 uint8_t TXData = 0;
60 uint8_t RXData = 0;
61 
62 int main(void)
63 {
64  EUSCI_B_SPI_initMasterParam spiMasterConfig =
65  {
66  EUSCI_B_SPI_CLOCKSOURCE_ACLK, // ACLK Clock Source
67  32000, // ACLK = 32kHz
68  16000, // SPICLK = 16kHz
69  EUSCI_B_SPI_MSB_FIRST, // MSB First
70  EUSCI_B_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT, // Phase
71  EUSCI_B_SPI_CLOCKPOLARITY_INACTIVITY_HIGH, // High polarity
72  EUSCI_B_SPI_3PIN // 3Wire SPI Mode
73  };
74 
75  WDT_hold(WDT_BASE);
76 
77  // Setting P1.5, P1.6 and P1.7 as SPI pins.
78  GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
79  GPIO_PIN5 | GPIO_PIN6 | GPIO_PIN7,
80  GPIO_PRIMARY_MODULE_FUNCTION);
81 
82  // Setting P1.4 as LED Pin
83  GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN4);
84  GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN4);
85 
86  // Setting the DCO to use the internal resistor. DCO will be at 16.384MHz
87  // ACLK is at 32kHz
88  CS_setupDCO(CS_INTERNAL_RESISTOR);
89 
90  // Configure and enable the SPI peripheral
91  EUSCI_B_SPI_initMaster(EUSCI_B0_BASE, &spiMasterConfig);
92  EUSCI_B_SPI_enable(EUSCI_B0_BASE);
93 
94  // Put the first byte in the transfer buffer
95  EUSCI_B_SPI_transmitData(EUSCI_B0_BASE, TXData);
96 
97  EUSCI_B_SPI_enableInterrupt(EUSCI_B0_BASE, EUSCI_B_SPI_RECEIVE_INTERRUPT);
98 
99  // Go into LPM0 with interrupts enabled
100  __bis_SR_register(LPM0_bits | GIE);
101 }
102 
103 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
104 #pragma vector=USCI_B0_VECTOR
105 __interrupt
106 #elif defined(__GNUC__)
107 __attribute__((interrupt(USCI_B0_VECTOR)))
108 #endif
109 void USCI_B0_ISR(void)
110 {
111  switch(__even_in_range(UCB0IV, USCI_SPI_UCTXIFG))
112  {
113  case USCI_NONE: break;
114  case USCI_SPI_UCRXIFG:
115  // Read what slave sent. Should be same as transferred. If there
116  // is a mismatch turn on the LED
117  RXData = EUSCI_B_SPI_receiveData(EUSCI_B0_BASE);
118  if(RXData != TXData)
119  {
120  GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN4);
121  while(1);
122  }
123  EUSCI_B_SPI_transmitData(EUSCI_B0_BASE, ++TXData);
124  break;
125  case USCI_SPI_UCTXIFG:
126  break;
127  default: break;
128  }
129 }
130 
uint8_t RXData
int main(void)
void USCI_B0_ISR(void)
uint8_t TXData
GPIO_setOutputHighOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)